// 生成模拟公司数据
const generateCompanies = (count) => {
  const companies = []
  const types = ['科技', '金融', '医疗', '教育', '制造', '零售', '物流', '能源', '传媒', '咨询']
  const sizes = ['小型', '中型', '大型', '超大型']
  const statuses = ['活跃', '非活跃', '暂停']
  
  const cities = ['北京', '上海', '广州', '深圳', '杭州', '南京', '成都', '武汉', '西安', '苏州', 
                '重庆', '天津', '青岛', '宁波', '厦门', '长沙', '郑州', '济南', '合肥', '福州']
  
  for (let i = 1; i <= count; i++) {
    const type = types[Math.floor(Math.random() * types.length)]
    const size = sizes[Math.floor(Math.random() * sizes.length)]
    const city = cities[Math.floor(Math.random() * cities.length)]
    
    companies.push({
      id: i,
      name: `${city}${type}企业${i.toString().padStart(3, '0')}`,
      shortName: `${type}${i.toString().padStart(3, '0')}`,
      type,
      size,
      status: statuses[Math.floor(Math.random() * statuses.length)],
      address: `${city}市${['高新区', '经济开发区', '中央商务区', '软件园'][Math.floor(Math.random() * 4)]}${Math.floor(Math.random() * 100) + 1}号`,
      contactPerson: `联系人${i}`,
      contactPhone: `1${Math.floor(Math.random() * 10)}${Array.from({length: 9}, () => Math.floor(Math.random() * 10)).join('')}`,
      email: `company${i}@example.com`,
      website: `https://www.company${i}.com`,
      description: `这是一家${size}${type}公司，位于${city}市，主要从事${type}相关业务。`,
      createTime: new Date(Date.now() - Math.floor(Math.random() * 1000) * 86400000).toISOString(),
      updateTime: new Date(Date.now() - Math.floor(Math.random() * 100) * 86400000).toISOString(),
      equipmentCount: Math.floor(Math.random() * 100),
      userCount: Math.floor(Math.random() * 50) + 5
    })
  }
  
  return companies
}

// 公司相关的模拟数据
export function setupCompanyMock(mock) {
  // 模拟的公司数据
  const companies = generateCompanies(60)
  
  // 获取公司列表
  mock.onGet('/api/company/list').reply((config) => {
    const { page = 1, pageSize = 10, keyword = '', type = '', status = '' } = config.params || {}
    
    let filteredCompanies = [...companies]
    
    // 按类型过滤
    if (type) {
      filteredCompanies = filteredCompanies.filter(
        company => company.type === type
      )
    }
    
    // 按状态过滤
    if (status) {
      filteredCompanies = filteredCompanies.filter(
        company => company.status === status
      )
    }
    
    // 关键词过滤
    if (keyword) {
      filteredCompanies = filteredCompanies.filter(
        company => company.name.includes(keyword) || 
                  company.shortName.includes(keyword) || 
                  company.description.includes(keyword) ||
                  company.address.includes(keyword)
      )
    }
    
    // 分页
    const total = filteredCompanies.length
    const start = (page - 1) * pageSize
    const end = start + Number(pageSize)
    const pageData = filteredCompanies.slice(start, end)
    
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: {
          list: pageData,
          pagination: {
            page: Number(page),
            pageSize: Number(pageSize),
            total
          }
        }
      }
    ]
  })
  
  // 获取公司类型列表
  mock.onGet('/api/company/types').reply(() => {
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: ['科技', '金融', '医疗', '教育', '制造', '零售', '物流', '能源', '传媒', '咨询']
      }
    ]
  })
  
  // 获取公司详情
  mock.onGet(/\/api\/company\/detail\/\d+/).reply((config) => {
    const id = parseInt(config.url.split('/').pop())
    const company = companies.find(c => c.id === id)
    
    if (company) {
      return [
        200,
        {
          code: 200,
          message: 'success',
          data: company
        }
      ]
    } else {
      return [
        200,
        {
          code: 404,
          message: '公司不存在',
          data: null
        }
      ]
    }
  })
} 